import token from '../../api/token'
import {
  SET_TOKEN,
  DESTROY_TOKEN,
  DESTROY_ACCESS_TOKEN,
  SHOW_SUCCESS_MSG,
  SHOW_ERROR_MSG
} from '../mutation-types'

// initial state
const state = {
  accessToken: window.sessionStorage.accessToken, //访问令牌
  refreshToken: window.localStorage.refreshToken, //刷新令牌
  expiresIn: window.sessionStorage.expiresIn, //访问令牌有效期秒
  createTime: window.sessionStorage.createTime, //令牌创建时间戳
  authenticationStates: (window.sessionStorage.accessToken || false) || (window.localStorage.refreshToken || false), //认证状态
  // authenticationError: '', //认证时发送的错误
}

// getters
const getters = {
  authenticationStates: state => state.authenticationStates,
  // authenticationError: state => state.authenticationError,
}

// actions
const actions = {
  //请求获取身份令牌
  reqGetAccessToken({
    commit,
    state
  }, payload) {
    token.getAccessToken(payload,
      (result) => {
        if (result.states == 200) {
          commit(SET_TOKEN, {
            token: result.results
          });
          commit(SHOW_SUCCESS_MSG, {
            message: '登录成功。',
            duration: 1000,
            needRedirect: true,
            redirectPathName: 'Main'
          });
        } else if (result.states == 403) {
          // commit('setAuthenticationError', {
          //   authenticationError: '账号密码不匹配，无法登录。'
          // });
          commit(SHOW_ERROR_MSG, {
            message: '账号密码不匹配，无法登录。'
          });
        }
      },
      (err) => {
        console.log(err);
      }
    )
  },
  //注销
  logout({
    commit
  }) {
    commit(DESTROY_TOKEN);
    commit(SHOW_SUCCESS_MSG, {
      message: '注销成功，即将返回登录页面。',
      needRedirect: true,
      redirectPathName: 'Login'
    });
  }
}

// mutations
const mutations = {
  //设置令牌
  [SET_TOKEN](state, {
    token
  }) {
    //本地存储
    window.sessionStorage.accessToken = token.access_token; //访问令牌
    window.localStorage.refreshToken = token.refresh_token; //刷新令牌
    window.sessionStorage.expiresIn = token.expires_in; //访问令牌有效期秒
    window.sessionStorage.createTime = token.create_time; //令牌创建时间戳

    state.accessToken = token.access_token;
    state.refreshToken = token.refresh_token;
    state.expiresIn = token.expires_in;
    state.createTime = token.create_time;
    state.authenticationStates = true;
    // state.authenticationError = '';
  },
  //销毁身份令牌
  [DESTROY_ACCESS_TOKEN](state) {
    window.sessionStorage.removeItem("accessToken");
    window.sessionStorage.removeItem("expiresIn");
    window.sessionStorage.removeItem("createTime");

    state.accessToken = undefined;
    state.expiresIn = undefined;
    state.createTime = undefined;
    state.authenticationStates = false;
  },
  //销毁令牌
  [DESTROY_TOKEN](state) {
    window.sessionStorage.removeItem("accessToken");
    window.localStorage.removeItem("refreshToken");
    window.sessionStorage.removeItem("expiresIn");
    window.sessionStorage.removeItem("createTime");

    state.accessToken = undefined;
    state.refreshToken = undefined;
    state.expiresIn = undefined;
    state.createTime = undefined;
    state.authenticationStates = false;
  },

  // setAuthenticationError(state, {
  //   authenticationError
  // }) {
  //   state.authenticationStates = false;
  //   state.authenticationError = authenticationError;
  // }
}

export default {
  state,
  getters,
  actions,
  mutations
}
